package cn.zhangxin.pethome.system.shop.service.impl;

import cn.zhangxin.pethome.base.query.BaseQuery;
import cn.zhangxin.pethome.constants.BaseConstants;
import cn.zhangxin.pethome.system.employee.domain.Employee;
import cn.zhangxin.pethome.system.employee.mapper.EmployeeMapper;
import cn.zhangxin.pethome.system.shop.domain.Shop;
import cn.zhangxin.pethome.system.shop.mapper.ShopMapper;
import cn.zhangxin.pethome.system.shop.query.ShopQuery;
import cn.zhangxin.pethome.system.shop.service.IShopService;
import cn.zhangxin.pethome.base.service.impl.BaseServiceImpl;
import cn.zhangxin.pethome.utils.AuthService;
import cn.zhangxin.pethome.utils.BusinessException;
import cn.zhangxin.pethome.utils.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zx
 * @since 2023-01-07
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private AuthService authService;

    @Override
    public void add(Shop shop) {
        if(shop.getName()==null){
            throw new BusinessException("请输入店铺名");
        }

        if (shop.getTel()==null){
            throw new BusinessException("请输入店铺座机");
        }
        if (shop.getAddress()==null){
            throw new BusinessException("请输入店铺地址");
        }
        if(shop.getLogo()==null || shop.getLogo().equals("")){
            throw new BusinessException("请上传至少一张店铺logo");
        }

        Boolean authentication = authService.authentication(shop.getName());//调用方法判断店铺名称是否违规 如果违规返回false
        if(!authentication){
            throw new BusinessException("店铺名称违规!!!");
        }

        //创建一个query
        ShopQuery shopQuery = new ShopQuery();
        //将店铺名设置到query中
        shopQuery.setName(shop.getName());
        //去除数据库中已经被驳回的店铺名称数据
        shopQuery.setNotState(BaseConstants.Shop.STATE_REJECT_AUDIT);
        //查询t_shop表中包含该店铺名的店铺的条数 为1就不能添加
        Long count = shopMapper.findByQueryCount(shopQuery);
        if (count==1){
            throw new BusinessException("该["+shop.getName()+"]店铺已存在");
        }


        //设置店铺注册时间
        shop.setRegisterTime(new Date());
        //设置店铺的状态
        shop.setState(BaseConstants.Shop.STATE_WAIT_AUTID);

        //从shop中获得admin对象
        Employee admin = shop.getAdmin();
        //设置管理员的状态 审核没通过设置为锁定
        admin.setState(BaseConstants.Employee.STATE_LOCK);
        //添加店铺管理员
        employeeMapper.add(admin);
        //获得店铺管理员的id
        Long adminId = admin.getId();
        //为店铺设置管理员关联的字段（管理员id）
        shop.setAdminId(adminId);

        //添加店铺
        shopMapper.add(shop);

        //新增shop中获得返回的主键
        Long shopId = shop.getId();

        //为店铺管理员设置关联店铺的主键字段
        admin.setShopId(shopId);
        //修改店铺管理员,将关联店铺主键添加进去
        employeeMapper.update(admin);

    }

}
